package nuist.hanniko.stu.webbackend.controller.api;

import nuist.hanniko.stu.webbackend.service.impls.DeviceService;
import nuist.hanniko.stu.webbackend.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/device/brief")
public class DeviceBriefController {

    private final DeviceService deviceService;

    @Autowired
    public DeviceBriefController(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @GetMapping("all")
    @ResponseBody
    public ResponseData getAllDevices() {
        ResponseData responseData = new ResponseData();

        responseData.setData(deviceService.getAllDevice());

        return responseData;
    }

    @GetMapping("index")
    @ResponseBody
    public ResponseData getDeviceByIndex(@RequestParam(name = "start") int start,
                                         @RequestParam(name = "length") int length) {
        ResponseData responseData = new ResponseData();

        if (start < 0 || length <= 0) {
            responseData.setStatus(false);
            responseData.setMessage("argument format error");
        } else {
            responseData.setData(deviceService.getDeviceByIndex(start, length));
        }

        return responseData;
    }
}
